package pl.elka.simulator.core.configurationCommand.impl;

import java.util.List;

import pl.elka.simulator.core.Node;
import pl.elka.simulator.core.Position;
import pl.elka.simulator.core.configurationCommand.ConfigurationCommandType;
import pl.elka.simulator.core.configurationCommand.ICommandParam;
import pl.elka.simulator.core.configurationCommand.IConfigurationCommand;
import pl.elka.simulator.core.configurationCommand.ables.IAbleToConfiguration;
import pl.elka.simulator.core.configurationCommand.ables.IAbleToNodeConfiguration;
import pl.elka.simulator.core.configurationCommand.impl.param.PositionCommandParam;

public class DeleteNodeConfigurationCommand implements IConfigurationCommand{

	private ICommandParam param;
	
	@Override
	public IConfigurationCommand newInstance() {	
		return new DeleteNodeConfigurationCommand();
	}

	@Override
	public void setParam(ICommandParam param) {
		this.param = param;
		
	}

	@Override
	public ConfigurationCommandType getType() {		
		return ConfigurationCommandType.DeleteNodeConfigurationCommand;
	}

	@Override
	public ICommandParam getParam() {		
		return param;
	}
	@Override
	public void execute(IAbleToConfiguration able){
		IAbleToNodeConfiguration na = (IAbleToNodeConfiguration) able;
				
		List<Node> nodes = na.getNodes();
		
		Position position = CommandUtils.getPosition(this);
		Node toDelete = CommandUtils.finNodeByPosition(nodes,position);
		if(toDelete!=null){
			nodes.remove(toDelete);
			return;
		}
		
		na.setNodes(nodes);
	}

	private Position getPosition() {
		return ((PositionCommandParam)getParam()).getPosition();
	}
}
